package com.aceitunaproject.server.message.processor;

import java.util.logging.Logger;

import com.aceitunaproject.server.player.Player;
import com.aceitunaproject.shared.exceptions.gamerules.IllegalActionException;
import com.aceitunaproject.shared.message.Message;
import com.aceitunaproject.shared.message.concrete.RecruitUnitRequest;

import com.aceitunaproject.shared.message.concrete.RuleViolationEvent;

/**
 * Handles the "Recruit Unit" action request. Builds an unit in the given
 * Settlement, if the action is legal
 * 
 * @author Federico Bergstein
 * 
 */
public class RecruitUnitMessageProcessor implements MessageProcessor {

	private static Logger logger = Logger
			.getLogger(RecruitUnitMessageProcessor.class.getName());

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void processMessage(Player player, Message message) {

		RecruitUnitRequest buildRequest = (RecruitUnitRequest) message;

		// TODO: Implement single logging
		logger.info("ordered to Recruit Unit " + buildRequest.getName()
				+ " with settlement " + buildRequest.getElementId());
				
		try {

			player.recruitUnit(buildRequest.getElementId(), buildRequest.getName(),
					buildRequest.getPersons());

		} catch (IllegalActionException e) {

			RuleViolationEvent ruleViolation = new RuleViolationEvent();
			ruleViolation.setIllegalActionMessageID(message.getId());
			ruleViolation.setRuleViolations(e);
			player.sendObjectMessage(ruleViolation);
		}

	}

}
